package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    private ObjectMapper mapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String method = request.getParameter("method");
        if ("selectByPage".equals(method)){
            selectByPage(request,response);
        }else if ("addStudent".equals(method)){
            addStudent(request,response);
        }else if ("updateStudent".equals(method)){
            updateStudent(request,response);
        }else if ("deleteStudent".equals(method)){
            deleteStudent(request,response);
        }

    }

    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String number = request.getParameter("number");
        StudentService studentService = new StudentServiceImpl();
        int count = studentService.deleteStudent(number);
        response.getWriter().write(count+"");
    }

    private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Student student = new Student();
        try {
            BeanUtils.populate(student,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        StudentService studentService = new StudentServiceImpl();
        int count = studentService.updateStudent(student);
        response.getWriter().write(count+"");
    }

    private void addStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Student student = new Student();
        try {
            BeanUtils.populate(student,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        StudentService studentService = new StudentServiceImpl();
        int count = studentService.addStudent(student);
        response.getWriter().write(count+"");
    }

    private void selectByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        StudentService studentService = new StudentServiceImpl();
        PageInfo<Student> info = studentService.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        mapper.writeValue(response.getWriter(),info);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
